const express = require('express');
const router = express.Router();
const { post } = require('./../util/http');
const { checkPort, checkIp } = require('./../util/common')

// 获取ssh和ping是否开启
router.all('/GetSshInfo', (req, res) => {
    post('GetSshInfo').then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

// 设置是否禁ping
router.all('/SetPing', (req, res) => {
    let params = req.body;
    if (typeof params.status === 'undefined' || (params.status.toString() !== '1' && params.status.toString() !== '0')) {
        res.json({ 'status': false, 'msg': 'status错误' });
        return;
    }
    // 0=>启用禁ping 1=>关闭禁ping
    post('SetPing', {
        status: params.status
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

// 设置是否开启ssh
router.all('/SetSshStatus', (req, res) => {
    let params = req.body;
    if (typeof params.status === 'undefined' || (params.status.toString() !== '1' && params.status.toString() !== '0')) {
        res.json({ 'status': false, 'msg': 'status错误' });
        return;
    }
    // 0=>启用ssh 1=>关闭ssh
    post('SetSshStatus', {
        status: params.status
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

// 设置ssh端口
router.all('/SetSshPort', (req, res) => {
    let params = req.body;
    if (typeof params.port === 'undefined' || !checkPort(params.port)) {
        res.json({ 'status': false, 'msg': '端口号错误' });
        return;
    }
    post('SetSshPort', {
        port: params.port
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

// 获取防火墙列表
router.all('/GetData', (req, res) => {
    let params = req.body;
    post('GetData', {
        tojs: 'firewall.get_list',
        table: 'firewall',
        order: typeof params.order != 'undefined' && params.order !== '' ? params.order : 'id desc',
        limit: typeof params.limit != 'undefined'? params.limit : 10,
        p: typeof params.p != 'undefined'? params.p : 1,
        search: typeof params.search != 'undefined' ? params.search : '',
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

// 添加放行端口
router.all('/AddAcceptPort', (req, res) => {
    let params = req.body;
    if (typeof params.port === 'undefined' || !checkPort(params.port)) {
        res.json({ 'status': false, 'msg': '端口号错误' });
        return;
    }
    post('AddAcceptPort', {
        type: 'port',
        port: params.port,
        ps: typeof params.ps != 'undefined' ? params.port : ''
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

// 删除放行端口
router.all('/DelAcceptPort', (req, res) => {
    let params = req.body;
    if (typeof params.port === 'undefined' || !checkPort(params.port)) {
        res.json({ 'status': false, 'msg': '端口号错误' });
        return;
    }
    if (typeof params.id === 'undefined') {
        res.json({ 'status': false, 'msg': '缺少参数id' });
        return;
    }
    post('DelAcceptPort', {
        port: params.port,
        id: params.id,
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

// 添加屏蔽ip
router.all('/AddDropAddress', (req, res) => {
    let params = req.body;
    if (typeof params.port === 'undefined' || !checkIp(params.port)) {
        res.json({ 'status': false, 'msg': 'ip地址格式错误' });
        return;
    }
    post('AddDropAddress', {
        type: 'address',
        port: params.port,
        ps: typeof params.ps != 'undefined' ? params.port : ''
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

// 删除屏蔽ip
router.all('/DelDropAddress', (req, res) => {
    let params = req.body;
    if (typeof params.port === 'undefined' || !checkIp(params.port)) {
        res.json({ 'status': false, 'msg': 'ip地址格式错误' });
        return;
    }
    if (typeof params.id === 'undefined') {
        res.json({ 'status': false, 'msg': '缺少参数id' });
        return;
    }
    post('DelDropAddress', {
        id: params.id,
        port: params.port,
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

// 获取操作日志
router.all('/GetLog', (req, res) => {
    let params = req.body;
    post('GetLog', {
        tojs: 'firewall.get_log_list',
        table: 'logs',
        order: typeof params.order != 'undefined' && params.order !== '' ? params.order : 'id desc',
        limit: typeof params.limit != 'undefined'? params.limit : 10,
        p: typeof params.p != 'undefined'? params.p : 1,
        search: typeof params.search != 'undefined' ? params.search : '',
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

// 清空操作日志
router.all('/DelClose', (req, res) => {
    post('DelClose').then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

module.exports = router;